<template>
  <el-dialog
    :title="!dataForm.orderId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="订单商品ID" prop="productId">
      <el-input v-model="dataForm.productId" placeholder="订单商品ID"></el-input>
    </el-form-item>
    <el-form-item label="商品名称" prop="productName">
      <el-input v-model="dataForm.productName" placeholder="商品名称"></el-input>
    </el-form-item>
    <el-form-item label="客户ID" prop="customerId">
      <el-input v-model="dataForm.customerId" placeholder="客户ID"></el-input>
    </el-form-item>
    <el-form-item label="商户ID" prop="merchantId">
      <el-input v-model="dataForm.merchantId" placeholder="商户ID"></el-input>
    </el-form-item>
    <el-form-item label="商品数量" prop="productNum">
      <el-input v-model="dataForm.productNum" placeholder="商品数量"></el-input>
    </el-form-item>
    <el-form-item label="商品价格" prop="productPrice">
      <el-input v-model="dataForm.productPrice" placeholder="商品价格"></el-input>
    </el-form-item>
    <el-form-item label="订单状态" prop="orderState">
      <el-input v-model="dataForm.orderState" placeholder="订单状态"></el-input>
    </el-form-item>
    <el-form-item label="商品图片" prop="productImg">
      <el-input v-model="dataForm.orderState" placeholder="商品图片"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          orderId: 0,
          productId: '',
          productName: '',
          customerId: '',
          merchantId: '',
          productNum: '',
          productPrice: '',
          orderState: '',
          productImg: ''
        },
        dataRule: {
          productId: [
            { required: true, message: '订单商品ID不能为空', trigger: 'blur' }
          ],
          productName: [
            { required: true, message: '商品名称不能为空', trigger: 'blur' }
          ],
          customerId: [
            { required: true, message: '客户ID不能为空', trigger: 'blur' }
          ],
          merchantId: [
            { required: true, message: '商户ID不能为空', trigger: 'blur' }
          ],
          num: [
            { required: true, message: '商品数量不能为空', trigger: 'blur' }
          ],
          productPrice: [
            { required: true, message: '订单总价不能为空', trigger: 'blur' }
          ],
          orderState: [
            { required: true, message: '订单状态不能为空', trigger: 'blur' }
          ],
          productImg: [
            { required: true, message: '商品图片不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.orderId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.orderId) {
            this.$http({
              url: this.$http.adornUrl(`/shop/order/info/${this.dataForm.orderId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.productId = data.order.productId
                this.dataForm.productName = data.order.productName
                this.dataForm.customerId = data.order.customerId
                this.dataForm.merchantId = data.order.merchantId
                this.dataForm.productNum = data.order.productNum
                this.dataForm.productPrice = data.order.productPrice
                this.dataForm.orderState = data.order.orderState
                this.dataForm.productImg = data.order.productImg
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/shop/order/${!this.dataForm.orderId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'orderId': this.dataForm.orderId || undefined,
                'productId': this.dataForm.productId,
                'productName': this.dataForm.productName,
                'customerId': this.dataForm.customerId,
                'merchantId': this.dataForm.merchantId,
                'productNum': this.dataForm.productNum,
                'productPrice': this.dataForm.productPrice,
                'orderState': this.dataForm.orderState,
                'productImg': this.dataForm.productImg
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
